package com.yongninger.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.UserDao;
import com.yongninger.vo.UserVo;

public class AddActivity extends Activity {
	private UserDao userDao;
	private CheckBox umenu11;
	private CheckBox umenu12;
	private CheckBox umenu21;
	private CheckBox umenu22;
	private CheckBox umenu31;
	private CheckBox umenu32;
	private CheckBox umenu41;
	private CheckBox umenu42;
	private CheckBox umenu51;
	private CheckBox umenu52;
	private CheckBox umenu61;
	private CheckBox umenu62;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_add);
		userDao = new UserDao(getApplicationContext());
		umenu12 = (CheckBox) findViewById(R.id.umenu12);
		umenu12.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu11 = (CheckBox) findViewById(R.id.umenu11);
					umenu11.setChecked(true);
				}
			}
		});
		umenu22 = (CheckBox) findViewById(R.id.umenu22);
		umenu22.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu21 = (CheckBox) findViewById(R.id.umenu21);
					umenu21.setChecked(true);
				}
			}
		});
		umenu32 = (CheckBox) findViewById(R.id.umenu32);
		umenu32.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu31 = (CheckBox) findViewById(R.id.umenu31);
					umenu31.setChecked(true);
				}
			}
		});
		umenu42 = (CheckBox) findViewById(R.id.umenu42);
		umenu42.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu41 = (CheckBox) findViewById(R.id.umenu41);
					umenu41.setChecked(true);
				}
			}
		});
		umenu52 = (CheckBox) findViewById(R.id.umenu52);
		umenu52.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu51 = (CheckBox) findViewById(R.id.umenu51);
					umenu51.setChecked(true);
				}
			}
		});
		umenu62 = (CheckBox) findViewById(R.id.umenu62);
		umenu62.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu61 = (CheckBox) findViewById(R.id.umenu61);
					umenu61.setChecked(true);
				}
			}
		});
		
		
		umenu11 = (CheckBox) findViewById(R.id.umenu11);
		umenu11.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu12 = (CheckBox) findViewById(R.id.umenu12);
					umenu12.setChecked(false);
				}
			}
		});
		umenu21 = (CheckBox) findViewById(R.id.umenu21);
		umenu21.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu22 = (CheckBox) findViewById(R.id.umenu22);
					umenu22.setChecked(false);
				}
			}
		});
		umenu31 = (CheckBox) findViewById(R.id.umenu31);
		umenu31.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu32 = (CheckBox) findViewById(R.id.umenu32);
					umenu32.setChecked(false);
				}
			}
		});
		umenu41 = (CheckBox) findViewById(R.id.umenu41);
		umenu41.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu42 = (CheckBox) findViewById(R.id.umenu42);
					umenu42.setChecked(false);
				}
			}
		});
		umenu51 = (CheckBox) findViewById(R.id.umenu51);
		umenu51.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu52 = (CheckBox) findViewById(R.id.umenu52);
					umenu52.setChecked(false);
				}
			}
		});
		umenu61 = (CheckBox) findViewById(R.id.umenu61);
		umenu61.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu62 = (CheckBox) findViewById(R.id.umenu62);
					umenu62.setChecked(false);
				}
			}
		});
		
		Button addBtn = (Button) this.findViewById(R.id.add);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 保存数据到数据库
				if (save()) {
					Toast.makeText(getApplicationContext(), "保存成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(AddActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "保存失败", 2).show();
				}
			}
		};
		addBtn.setOnClickListener(onClickListener);
	}

	private boolean save() {
		EditText uname = (EditText) findViewById(R.id.uname);
		EditText userPwd = (EditText) findViewById(R.id.userPwd);
		userDao = new UserDao(getApplicationContext());
		UserVo userVo = new UserVo();
		userVo.setUname(uname.getText().toString());
		userVo.setUpwd(userPwd.getText().toString());
		userVo.setUtype("1");
		userVo.setUmenu1("1发货单");
		
		if(umenu11.isChecked()){
			userVo.setUmenu11("1");
		}else{
			userVo.setUmenu11("0");
		}
		if(umenu12.isChecked()){
			userVo.setUmenu12("1");
		}else{
			userVo.setUmenu12("0");
		}
		
		if(umenu21.isChecked()){
			userVo.setUmenu21("1");
		}else{
			userVo.setUmenu21("0");
		}
		if(umenu22.isChecked()){
			userVo.setUmenu22("1");
		}else{
			userVo.setUmenu22("0");
		}
		
		if(umenu31.isChecked()){
			userVo.setUmenu31("1");
		}else{
			userVo.setUmenu31("0");
		}
		if(umenu32.isChecked()){
			userVo.setUmenu32("1");
		}else{
			userVo.setUmenu32("0");
		}
		
		if(umenu41.isChecked()){
			userVo.setUmenu41("1");
		}else{
			userVo.setUmenu41("0");
		}
		if(umenu42.isChecked()){
			userVo.setUmenu42("1");
		}else{
			userVo.setUmenu42("0");
		}
		
		if(umenu51.isChecked()){
			userVo.setUmenu51("1");
		}else{
			userVo.setUmenu51("0");
		}
		if(umenu52.isChecked()){
			userVo.setUmenu52("1");
		}else{
			userVo.setUmenu52("0");
		}

		if(umenu61.isChecked()){
			userVo.setUmenu61("1");
		}else{
			userVo.setUmenu61("0");
		}
		if(umenu62.isChecked()){
			userVo.setUmenu62("1");
		}else{
			userVo.setUmenu62("0");
		}
		return userDao.insertUserVo(userVo);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(AddActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
